home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 1812 / 1812.xpi / chrome / colt.jar / content / colt.js next >
Text File  |  2009-08-28  |  13KB  |  386 lines

  1. var objCoLT = {
  2.     ConsoleService : Components.classes['@mozilla.org/consoleservice;1'].getService(Components.interfaces.nsIConsoleService),
  3.     PrefBranch : Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("colt."),
  4.     
  5.     // Preference names
  6.     PrefName_ShowCopyText        : "showcopytext",
  7.     PrefName_ShowCopyBoth        : "showcopyboth",
  8.     PrefName_ShowCopyPage        : "showcopypage",
  9.     PrefName_CustomFormatCount     : "custom.count",
  10.     PrefName_Version            : "version",
  11.  
  12.     // Option variables
  13.     ShowCopyText : false,
  14.     ShowCopyBoth : false,
  15.     ShowCopyPage : false,
  16.     
  17.     // Miscellaneous variables
  18.     RunOnce : false,
  19.     Version : "2.4", // Must be of the form "X.Y" or "X.YZ" (only 1 decimal)
  20.  
  21.     Log: function(aMessage)
  22.     {
  23.         this.ConsoleService.logStringMessage('CoLT: ' + aMessage);
  24.     },
  25.     
  26.     CopyBoth: function(formatIndex, type)
  27.     {
  28.         var url;
  29.         var text;
  30.         var titleAttr;
  31.         var pageTitle;
  32.         var pageURL;
  33.     
  34.         if(type == "page")
  35.         {
  36.             url = content.document.location;
  37.             text = content.document.title;
  38.             titleAttr = "";
  39.             pageTitle = "";
  40.             pageURL = "";
  41.         }
  42.         else
  43.         {
  44.             url = gContextMenu.linkURL;
  45.             text = gContextMenu.linkText();
  46.             titleAttr = document.popupNode.title;
  47.             pageTitle = content.document.title;
  48.             pageURL = content.document.location;
  49.         }
  50.         
  51.         var format = this.PrefBranch.getCharPref("custom." + formatIndex + ".format");
  52.         var myString = objCoLT.FormatString(format, text, url, titleAttr, pageTitle, pageURL);
  53.     
  54.         var result = objCoLT.PlaceOnClipboard(myString);
  55.         if(!result)
  56.             alert("ERROR: The link text and location were unable to be placed on the clipboard.");
  57.     },
  58.     
  59.     CopyBothAsRichText: function(type)
  60.     {
  61.         var richText = "<a href=\"";
  62.     
  63.         if(type == "page")
  64.             richText += content.document.location + "\">" + content.document.title + "</a>";
  65.         else
  66.             richText += gContextMenu.linkURL + "\">" + gContextMenu.linkText() + "</a>";
  67.         
  68.         var xfer = Components.classes["@mozilla.org/widget/transferable;1"].createInstance(Components.interfaces.nsITransferable);
  69.         xfer.addDataFlavor("text/html");
  70.     
  71.         var htmlString = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
  72.         htmlString.data = richText;
  73.         xfer.setTransferData("text/html", htmlString, richText.length * 2);
  74.     
  75.         var clipboard = Components.classes["@mozilla.org/widget/clipboard;1"].getService(Components.interfaces.nsIClipboard);
  76.         clipboard.setData(xfer, null, Components.interfaces.nsIClipboard.kGlobalClipboard);
  77.     },
  78.     
  79.     CopyLinkText: function()
  80.     {
  81.         var linkText = gContextMenu.linkText();
  82.         var result = this.PlaceOnClipboard(linkText);
  83.         if(!result)
  84.             alert("ERROR: The link text was unable to be placed on the clipboard.");
  85.     },
  86.  
  87.     FormatString: function(string, text, url, titleAttr, pageTitle, pageURL)
  88.     {
  89.         string = string.replace(/%[Tt]/g, text);
  90.         string = string.replace(/%[Uu]/g, url);
  91.         string = string.replace(/%[Nn]/g, this.GetNewLine());
  92.         string = string.replace(/%[Ii]/g, titleAttr);
  93.         string = string.replace(/%[Pp]/g, pageTitle);
  94.         string = string.replace(/%[Rr]/g, pageURL);
  95.         return string;
  96.     },
  97.  
  98.     GetNewLine: function()
  99.     {
  100.         var platform = navigator.platform.toLowerCase();
  101.     
  102.         if(platform.indexOf('win') != -1) // Windows
  103.             return "\r\n";
  104.     
  105.         else if(platform.indexOf('mac') != -1) // Mac
  106.             return "\r";
  107.     
  108.         else // *nix
  109.             return "\n";
  110.     },
  111.     
  112.     Init: function()
  113.     {
  114.         if(objCoLT.RunOnce == false)
  115.         {
  116.             objCoLT.RunOnce = true;
  117.     
  118.             objCoLT.Upgrade();
  119.             objCoLT.LoadPrefs();
  120.             objCoLT.UpdateContextMenu();
  121.     
  122.             var menu = document.getElementById("contentAreaContextMenu");
  123.             menu.addEventListener('popupshowing', objCoLT.UpdateContextMenu, false);
  124.         }
  125.     },
  126.  
  127.     IsPreferenceSet: function(pref)
  128.     {
  129.         if(pref)
  130.             return this.PrefBranch.prefHasUserValue(pref);
  131.     
  132.         return false;
  133.     },
  134.  
  135.     LoadPrefs: function()
  136.     {
  137.         var b = this.PrefBranch;
  138.         var initDefaults = false;
  139.     
  140.         this.ShowCopyText = b.getBoolPref(this.PrefName_ShowCopyText);
  141.         this.ShowCopyBoth = b.getBoolPref(this.PrefName_ShowCopyBoth);
  142.         this.ShowCopyPage = b.getBoolPref(this.PrefName_ShowCopyPage);
  143.         this.CustomFormatCount = b.getIntPref(this.PrefName_CustomFormatCount);
  144.     },
  145.  
  146.     MigrateTo23: function()
  147.     {
  148.         var stringBundle = document.getElementById("CLT-String-Bundle");
  149.     
  150.         // Create all the new preferences, migrating old settings as necessary
  151.         this.PrefBranch.setIntPref(this.PrefName_CustomFormatCount, 6);
  152.     
  153.         this.PrefBranch.setCharPref("custom.1.label", stringBundle.getString("CLT_DefaultLabelHTMLLink"));
  154.         this.PrefBranch.setCharPref("custom.1.format", "<a href=\"%U\">%T</a>");
  155.         this.PrefBranch.setBoolPref("custom.1.separator", false);
  156.         
  157.         this.PrefBranch.setCharPref("custom.2.label", stringBundle.getString("CLT_DefaultLabelPlainText"));
  158.         this.PrefBranch.setCharPref("custom.2.format", "%T - %U");
  159.         this.PrefBranch.setBoolPref("custom.2.separator", false);
  160.     
  161.         this.PrefBranch.setBoolPref("custom.3.separator", true);
  162.     
  163.         if(this.IsPreferenceSet("customlabel1") && this.IsPreferenceSet("customformat1"))
  164.         {
  165.             this.PrefBranch.setCharPref("custom.4.label", this.PrefBranch.getCharPref("customlabel1"));
  166.             this.PrefBranch.setCharPref("custom.4.format", this.PrefBranch.getCharPref("customformat1"));
  167.             this.PrefBranch.setBoolPref("custom.4.separator", false);
  168.     
  169.             if(this.PrefBranch.prefHasUserValue("customlabel1"))
  170.                 this.PrefBranch.clearUserPref("customlabel1");
  171.     
  172.             if(this.PrefBranch.prefHasUserValue("customformat1"))
  173.                 this.PrefBranch.clearUserPref("customformat1");
  174.         }
  175.         else
  176.         {
  177.             // If we can't migrate, set a default custom format
  178.             this.PrefBranch.setCharPref("custom.4.label", "BB Code");
  179.             this.PrefBranch.setCharPref("custom.4.format", "[url=%U]%T[/url]");
  180.             this.PrefBranch.setBoolPref("custom.4.separator", false);
  181.         }
  182.     
  183.         if(this.IsPreferenceSet("customformat2") && this.IsPreferenceSet("customlabel2"))
  184.         {
  185.             this.PrefBranch.setCharPref("custom.5.label", this.PrefBranch.getCharPref("customlabel2"));
  186.             this.PrefBranch.setCharPref("custom.5.format", this.PrefBranch.getCharPref("customformat2"));
  187.             this.PrefBranch.setBoolPref("custom.5.separator", false);
  188.     
  189.             if(this.PrefBranch.prefHasUserValue("customlabel2"))
  190.                 this.PrefBranch.clearUserPref("customlabel2");
  191.     
  192.             if(this.PrefBranch.prefHasUserValue("customformat2"))
  193.                 this.PrefBranch.clearUserPref("customformat2");
  194.         }
  195.         else
  196.         {
  197.             // If we can't migrate, set a default custom format
  198.             this.PrefBranch.setCharPref("custom.5.label", "FuseTalk");
  199.             this.PrefBranch.setCharPref("custom.5.format", "[L=%T]%U[/L]");
  200.             this.PrefBranch.setBoolPref("custom.5.separator", false);
  201.         }
  202.     
  203.         if(this.IsPreferenceSet("customformat3") && this.IsPreferenceSet("customlabel3"))
  204.         {
  205.             this.PrefBranch.setCharPref("custom.6.label", this.PrefBranch.getCharPref("customlabel3"));
  206.             this.PrefBranch.setCharPref("custom.6.format", this.PrefBranch.getCharPref("customformat3"));
  207.             this.PrefBranch.setBoolPref("custom.6.separator", false);
  208.     
  209.             if(this.PrefBranch.prefHasUserValue("customlabel3"))
  210.                 this.PrefBranch.clearUserPref("customlabel3");
  211.     
  212.             if(this.PrefBranch.prefHasUserValue("customformat3"))
  213.                 this.PrefBranch.clearUserPref("customformat3");
  214.         }
  215.         else
  216.         {
  217.             // If we can't migrate, set a default custom format
  218.             this.PrefBranch.setCharPref("custom.6.label", "Wikipedia");
  219.             this.PrefBranch.setCharPref("custom.6.format", "[%U %T]");
  220.             this.PrefBranch.setBoolPref("custom.6.separator", false);
  221.         }
  222.  
  223.         // Delete all other old preferences that we no longer use
  224.         if(this.IsPreferenceSet("submenu.custom1"))
  225.             this.PrefBranch.clearUserPref("submenu.custom1");
  226.     
  227.         if(this.IsPreferenceSet("submenu.custom2") && this.PrefBranch.prefHasUserValue("submenu.custom2"))
  228.             this.PrefBranch.clearUserPref("submenu.custom2");
  229.     
  230.         if(this.IsPreferenceSet("submenu.custom3") && this.PrefBranch.prefHasUserValue("submenu.custom3"))
  231.             this.PrefBranch.clearUserPref("submenu.custom3");
  232.     
  233.         if(this.IsPreferenceSet("submenu.html") && this.PrefBranch.prefHasUserValue("submenu.html"))
  234.             this.PrefBranch.clearUserPref("submenu.html");
  235.     
  236.         if(this.IsPreferenceSet("submenu.text") && this.PrefBranch.prefHasUserValue("submenu.text"))
  237.             this.PrefBranch.clearUserPref("submenu.text");
  238.     },
  239.     
  240.     MigrateTo24: function()
  241.     {
  242.         var count = this.PrefBranch.getIntPref(this.PrefName_CustomFormatCount);
  243.         for (var i=1; i <= count; i++)
  244.         {
  245.             var prefName = "custom." + i + ".richtext";
  246.             this.PrefBranch.setBoolPref(prefName, false);
  247.         }
  248.     },
  249.  
  250.     OptionsHaveUpdated: function()
  251.     {
  252.         this.LoadPrefs();
  253.         this.UpdateContextMenu();
  254.     },
  255.  
  256.     ParseVersion: function(version)
  257.     {
  258.         if(version)
  259.         {
  260.             var splitVersion = version.split(".");
  261.             var parsedVersion = splitVersion[0] + ".";
  262.     
  263.             for(var i=1; i<splitVersion.length; i++)
  264.             {
  265.                 parsedVersion += splitVersion[i];
  266.             }
  267.     
  268.             return parseFloat(parsedVersion);
  269.         }
  270.         else
  271.             return 0;
  272.     },
  273.  
  274.     PlaceOnClipboard: function(string)
  275.     {
  276.         var clipboard = Components.classes["@mozilla.org/widget/clipboardhelper;1"].getService(Components.interfaces.nsIClipboardHelper);
  277.         clipboard.copyString(string);
  278.         return true;
  279.     },
  280.  
  281.     PurgeContextSubMenu: function(nodeID)
  282.     {
  283.         // Remove all the menu items from the sub menu (since we will recreate them)
  284.         var popupmenu = document.getElementById(nodeID);
  285.         while(popupmenu.firstChild)
  286.             popupmenu.removeChild(popupmenu.firstChild);
  287.     },
  288.  
  289.     UpdateContextMenu: function()
  290.     {
  291.         // Only show the menu items if we're on a link and it's not a mailto link
  292.         var hiddenFlag = true;
  293.         if(gContextMenu && (gContextMenu.onLink && !gContextMenu.onMailtoLink))
  294.             hiddenFlag = false;
  295.     
  296.         var copyText = document.getElementById("CLT-Context-CopyLinkText");
  297.         var copyBothItem = document.getElementById("CLT-Context-CopyBoth");
  298.         var copyBothMenu = document.getElementById("CLT-Context-CopyBothMenu");
  299.         var copyPageItem = document.getElementById("CLT-Context-CopyPage");
  300.         var copyPageMenu = document.getElementById("CLT-Context-CopyPageMenu");
  301.     
  302.         copyText.hidden = (objCoLT.ShowCopyText) ? hiddenFlag : true;
  303.         copyBothItem.hidden = (objCoLT.ShowCopyBoth && (objCoLT.CustomFormatCount == 1)) ? hiddenFlag : true;
  304.         copyBothMenu.hidden = (objCoLT.ShowCopyBoth && (objCoLT.CustomFormatCount > 1)) ? hiddenFlag : true;
  305.     
  306.         // This time we default to false (we want to show the items more often than we want to hide them)
  307.         hiddenFlag = false;
  308.         if(gContextMenu && (gContextMenu.onLink || gContextMenu.onTextInput))
  309.             hiddenFlag = true;
  310.         
  311.         copyPageItem.hidden = (objCoLT.ShowCopyPage && (objCoLT.CustomFormatCount == 1)) ? hiddenFlag : true;
  312.         copyPageMenu.hidden = (objCoLT.ShowCopyPage && (objCoLT.CustomFormatCount > 1)) ? hiddenFlag : true;
  313.     },
  314.  
  315.     UpdateContextSubMenu: function(nodeID, type)
  316.     {
  317.         this.PurgeContextSubMenu(nodeID);
  318.     
  319.         var popupmenu = document.getElementById(nodeID);
  320.         
  321.         for(var i=1; i <= this.CustomFormatCount; i++)
  322.         {
  323.             var separatorPref = "custom." + i + ".separator";
  324.     
  325.             var isSeparator = false;
  326.             if(this.IsPreferenceSet(separatorPref))
  327.                 isSeparator = this.PrefBranch.getBoolPref(separatorPref);
  328.             
  329.             if(isSeparator)
  330.             {
  331.                 var menuseparator = document.createElement("menuseparator");
  332.                 popupmenu.appendChild(menuseparator);
  333.             }
  334.             else
  335.             {
  336.                 var isRichText = false;
  337.                 var richTextPref = "custom." + i + ".richtext";
  338.                 if(this.IsPreferenceSet(richTextPref))
  339.                     isRichText = this.PrefBranch.getBoolPref(richTextPref);
  340.     
  341.                 var labelPref = "custom." + i + ".label";
  342.                 var label = "";
  343.                 if(this.IsPreferenceSet(labelPref))
  344.                     label = this.PrefBranch.getCharPref(labelPref);
  345.     
  346.                 // Skip any weird occurances that don't have a label (shouldn't happen, but you never know)
  347.                 if(label && label != "")
  348.                 {
  349.                     var menuitem = document.createElement("menuitem");
  350.                     menuitem.setAttribute("label", label);
  351.     
  352.                     if(isRichText)
  353.                         menuitem.setAttribute("oncommand", "objCoLT.CopyBothAsRichText('" + type + "');");
  354.                     else
  355.                         menuitem.setAttribute("oncommand", "objCoLT.CopyBoth('" + i + "', '" + type + "');");
  356.     
  357.                     popupmenu.appendChild(menuitem);
  358.                 }
  359.             }
  360.         }
  361.     },
  362.  
  363.     Upgrade: function()
  364.     {
  365.         var previousVersion = 0;
  366.         
  367.         if(this.IsPreferenceSet(this.PrefName_Version))
  368.             previousVersion = this.ParseVersion(this.PrefBranch.getCharPref(this.PrefName_Version));
  369.     
  370.         var currentVersion = this.ParseVersion(this.Version);
  371.     
  372.         if(previousVersion != currentVersion)
  373.         {
  374.             if(previousVersion < this.ParseVersion("2.3"))
  375.                 this.MigrateTo23();
  376.     
  377.             if(previousVersion < this.ParseVersion("2.4"))
  378.                 this.MigrateTo24();
  379.  
  380.             this.PrefBranch.setCharPref(this.PrefName_Version, this.Version);
  381.         }
  382.     }
  383. };
  384.  
  385. window.addEventListener('load', objCoLT.Init, false);
  386.